﻿using System.Linq;
using System.Web.Mvc;
using CMSMVC4.Areas.Admin.Models;
using CMSMVC4.Filters;

namespace CMSMVC4.Areas.Admin.Controllers
{
    [Authorize(Roles = "Admin,Baner")]
    public class BanerController : Controller
    {
        readonly CMSMVC4Entities _db = new CMSMVC4Entities();
        //
        // GET: /Admin/Baner/
        [HttpGet]
        public ActionResult Index(string id)
        {
            ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/Baner").Rows[0]["Id"];
            var data = new CommandSql().GetBanerTop();
            ViewBag.id = id;
            return View(data);
        }

        //
        //GET: /Admin/Baner/Add
        [HttpGet]
        public ActionResult Add()
        {
            ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/Baner/Add").Rows[0]["Parrent"];
            //var top = _db.Baners.FirstOrDefault(x => x.Status == BanerStatus.Top);
            //if (top == null)
            //{
            //    var bntop = new Baner { Name = "", Status = BanerStatus.Top };
            //    _db.Baners.AddObject(bntop);
            //    _db.SaveChanges();
            //    top = _db.Baners.FirstOrDefault(x => x.Status == BanerStatus.Top);
            //}
            //var left = _db.Baners.FirstOrDefault(x => x.Status == BanerStatus.Left);
            //if (left == null)
            //{
            //    Baner bnleft = new Baner();
            //    bnleft.Name = "";
            //    bnleft.Status = BanerStatus.Left;
            //    _db.Baners.AddObject(bnleft);
            //    _db.SaveChanges();
            //    left = _db.Baners.FirstOrDefault(x => x.Status == BanerStatus.Left);
            //}
            //var lienhe = _db.Baners.FirstOrDefault(x => x.Status == BanerStatus.Lienhe);
            //if (lienhe == null)
            //{
            //    Baner bnlh = new Baner();
            //    bnlh.Name = "";
            //    bnlh.Status = BanerStatus.Lienhe;
            //    _db.Baners.AddObject(bnlh);
            //    _db.SaveChanges();
            //    lienhe = _db.Baners.FirstOrDefault(x => x.Status == BanerStatus.Lienhe);
            //}
            //BanerModel model = new BanerModel();
            //model.Idtop = top.Id;
            //model.Nametop = top.Name;
            //model.Urltop = top.Url;
            //model.Idleft = left.Id;
            //model.Nameleft = left.Name;
            //model.Urlleft = left.Url;
            //model.Idlienhe = lienhe.Id;
            //model.Namelienhe = lienhe.Name;
            //model.Urllienhe = lienhe.Url;

            return View();
        }

        [HttpPost]
        public ActionResult Add(BanerModel model)
        {
            if (model.Location == 2)
            {
                model.Type = 6;
            }

            var baner = new Baner
                            {
                                Url = model.Url,
                                Name = model.Name,
                                Status = 0,
                                Location = model.Location,
                                Type = model.Type
                            };
            _db.Baners.AddObject(baner);
            _db.SaveChanges();
            string id = "gioithieu";
            if (baner.Location == BanerType.DichVu)
            {
                id = "dichvu";
            }
            if (baner.Location == BanerType.LienHe)
            {
                id = "lienhe";
            }
            if (baner.Location == BanerType.LogoWeb)
            {
                id = "logo";
            }
            if (baner.Location == BanerType.ThietBi)
            {
                id = "thietbi";
            }
            if (baner.Location == BanerType.TinTuc)
            {
                id = "tintuc";
            }
            return RedirectToAction("Index","Baner",new{id = id});

        }

        [AllowAnonymous]
        [ChildActionOnly]
        public ActionResult BanerGioiThieu()
        {
            var data = new CommandSql().GetBanerGoiThieu();
            return PartialView(data);
        }

        [AllowAnonymous]
        [ChildActionOnly]
        public ActionResult BanerThietBi()
        {
            var date = new CommandSql().GetBanerThietBi();
            return PartialView(date);
        }

        [AllowAnonymous]
        [ChildActionOnly]
        public ActionResult BanerDichVu()
        {
            var date = new CommandSql().GetBanerDichVu();
            return PartialView(date);
        }

        [AllowAnonymous]
        [ChildActionOnly]
        public ActionResult BanerTinTuc()
        {
            var date = new CommandSql().GetBanerTinTuc();
            return PartialView(date);
        }

        [AllowAnonymous]
        [ChildActionOnly]
        public ActionResult BanerLienHe()
        {
            var date = new CommandSql().GetBanerLienHe();
            return PartialView(date);
        }

        public ActionResult ActiveGt(int id)
        {
            var data1 = _db.Baners.Where(x => x.Type == BanerType.GioiThieu).ToList();
            foreach (var baner in data1)
            {
                baner.Status = 0;
            }
            var data = _db.Baners.FirstOrDefault(x => x.Id == id);
            if (data != null) data.Status = 1;
            _db.SaveChanges();
            return RedirectToAction("Index");
        }


        public ActionResult ActiveTb(int id)
        {
            var data1 = _db.Baners.Where(x => x.Type == BanerType.ThietBi).ToList();
            foreach (var baner in data1)
            {
                baner.Status = 0;
            }
            var data = _db.Baners.FirstOrDefault(x => x.Id == id);
            if (data != null) data.Status = 1;
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult ActiveDv(int id)
        {
            var data1 = _db.Baners.Where(x => x.Type == BanerType.DichVu).ToList();
            foreach (var baner in data1)
            {
                baner.Status = 0;
            }
            var data = _db.Baners.FirstOrDefault(x => x.Id == id);
            if (data != null) data.Status = 1;
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult ActiveTt(int id)
        {
            var data1 = _db.Baners.Where(x => x.Type == BanerType.TinTuc).ToList();
            foreach (var baner in data1)
            {
                baner.Status = 0;
            }
            var data = _db.Baners.FirstOrDefault(x => x.Id == id);
            if (data != null) data.Status = 1;
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult ActiveLh(int id)
        {
            var data1 = _db.Baners.Where(x => x.Type == BanerType.LienHe).ToList();
            foreach (var baner in data1)
            {
                baner.Status = 0;
            }
            var data = _db.Baners.FirstOrDefault(x => x.Id == id);
            if (data != null) data.Status = 1;
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult Delete(int id)
        {
            return PartialView();
        }

        [HttpPost]
        public ActionResult Delete(int id, string url)
        {
            var data = _db.Baners.FirstOrDefault(x => x.Id == id);
            _db.Baners.DeleteObject(data);
            _db.SaveChanges();
            if (Request.IsAjaxRequest())
            {
                return JavaScript("window.location='"+Request.UrlReferrer.ToString()+"'");
            }
            return Redirect(Request.UrlReferrer.ToString());
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            var data = _db.Baners.FirstOrDefault(x => x.Id == id);
            if (data != null)
            {
                ViewBag.Url = data.Url;
                ViewBag.Name = data.Name;
            }
            return PartialView();
        }

        [HttpPost]
        public ActionResult Edit(int id, EditBaneModel model)
        {
            if (ModelState.IsValid)
            {
                var data = _db.Baners.SingleOrDefault(x => x.Id == id);
                if (data != null)
                {
                    if (data.Location != BanerType.LogoWeb && model.Type == -1)
                    {
                        ViewBag.Url = data.Url;
                        ViewBag.Name = data.Name;
                        ModelState.AddModelError("Type", "Hãy chọn trang!");
                        return PartialView(model);
                    }
                    
                    data.Url = model.Url;
                    data.Name = model.Name;
                    if (data.Location != BanerType.LogoWeb)
                    {
                        data.Type = model.Type;
                    }
                }
                _db.SaveChanges();
            }
            if (Request.IsAjaxRequest())
            {
                return JavaScript("window.location='/Admin/Baner/Index'");
            }
            return RedirectToAction("Index");
        }

        [AllowAnonymous]
        [ChildActionOnly]
        public ActionResult Logo()
        {
            Baner data = _db.Baners.FirstOrDefault(x => x.Location == BanerType.LogoWeb);
            if (data == null)
            {
                Baner r = new Baner();
                r.Url = "#";
                r.Location = BanerType.LogoWeb;
                r.Status = 0;
                r.Type = BanerStatus.Top;
                _db.Baners.AddObject(r);
                _db.SaveChanges();
                data = _db.Baners.FirstOrDefault(x => x.Status == BanerType.LogoWeb);
            }
            return PartialView(data);
        }
    }
}
